package com.hotau.member.service;

import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

import com.hotau.HotauActivity;
import com.hotau.HotauConstants;
import com.hotau.util.DialogUtil;
import com.hotau.util.HttpRequestAsynTask;

/**
 * 用户信息服务
 * 
 * @author liujia (liujia9234@126.com)
 * @data @2011-12-11
 * @version 1.0
 *          <ul>
 *          <li>1.0 创建</li>
 *          </ul>
 */
public class MemberService {

	public static final int ACC_OK = 1;
	public static final int ACC_FORBIDDEN_IN = 2;
	public static final int ACC_FORBIDDEN_OUT = 3;
	public static final int ACC_FREEZING = 4;
	public static final int ACC_FORBIDDEN_BOTH = 5;

	public static boolean isLogin(Context context) {
		SharedPreferences share = context.getSharedPreferences("MEMBER_USER", 0);
		String data = share.getString("login_user_info", "");
		if (!"".equals(data)) {
			try {
				JSONObject login_result = new JSONObject(data);
				if (login_result.getInt("result") == 1&& login_result.has("lastDate")) {
					long login_date = login_result.getLong("lastDate");
					if ((new Date().getTime() - login_date) < 30 * 60 * 1000) {
						login_result.put("lastDate", new Date().getTime());
						SharedPreferences.Editor shareEdit = share.edit();
						shareEdit.putString("login_user_info",login_result.toString());
						return true;
					}
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
			}
			Editor editor = share.edit();
			editor.remove("login_user_info");
			editor.commit();
		}
		return false;
		// String login_token = share.getString("login_token", "");
		// long login_date = share.getLong("login_date", 0);
		//
		// String login_user_info = share.getString("login_user_info", "");
		// try {
		// JSONObject login_result = new JSONObject(login_user_info);
		// if(login_result.getInt("result") == 0 &&
		// (login_result.getInt("errorCode")==3 ||
		// login_result.getInt("errorCode")==6)){
		// return true;
		// }
		// } catch (JSONException e) {
		// // TODO Auto-generated catch block
		// }
		//
		// if("".equals(login_token) || (new Date().getTime() -
		// login_date)>30*60*1000){
		// Editor editor = share.edit();
		// editor.remove("login_token");
		// editor.remove("login_date");
		// return false;
		// }
		// return true;
	}

	public static String getToken(Context context) {
		if (isLogin(context)) {
			SharedPreferences share = context.getSharedPreferences(
					"MEMBER_USER", 0);
			String data = share.getString("login_user_info", "");
			try {
				JSONObject login_result = new JSONObject(data);
				return login_result.getString("token");
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		return "";
	}

	public static void logout(Context context) {
//		SharedPreferences share = context.getSharedPreferences("MEMBER_USER", 0);
//		Editor editor1 = share.edit();
//		editor1.remove("account_user_info");
//		editor1.commit();
		if (isLogin(context)) {
			final HotauActivity csmbpActivity = (HotauActivity) context;
			HttpRequestAsynTask offLogin_task = new HttpRequestAsynTask(context) {
				@Override
				protected void doPostExecute(String result) {
					try {
						JSONObject offlogin_result = new JSONObject(result);
						Log.d("mytag",
								"offlogin_result="
										+ offlogin_result.getInt("result"));
						if (offlogin_result != null
								&& offlogin_result.getInt("result") == 1) {
							Log.d("mytag", "退出操作，准备删除本地东东");
							// Editor editor =
							// context.getSharedPreferences("MEMBER_USER",0).edit();
							// editor.remove("login_token");
							// editor.remove("login_date");
							// editor.commit();
							// csmbpActivity.toHome();
						}
					} catch (JSONException e) {
						DialogUtil.showDialog(context, "", "注销返回数据出错 ");
					}
				}
			};
			StringBuilder sb = new StringBuilder();
			sb.append("Form:token=" + getToken(context));
			sb.append(";terminalCode=" + HotauConstants.getTerminalCode(context));
			offLogin_task.setShowProgressDialog(false);
			Editor editor = context.getSharedPreferences("MEMBER_USER", 0).edit();
			// if(isActivate(context) && !isFreezing(context)){
			// offLogin_task.execute(HNAPAYConstants.MEMBER_LOGOUT_URL,sb.toString(),HttpUtil.UTF8_ENCODING);
			// editor.remove("login_user_info");
			// editor.commit();
			// }else{
			// editor.remove("login_user_info");
			// }
			editor.remove("login_user_info");
			editor.commit();
			csmbpActivity.toHome();
		}
	}

	public static String getLastLoginUserCode(Context context) {
		SharedPreferences share = context
				.getSharedPreferences("MEMBER_USER", 0);
		return share.getString("last_login_usercode", "");
	}

	public static boolean isActivate(Context context) {
		int accStatus = getAccStatus(context);
		if (ACC_OK == accStatus) {
			return true;
		}
		return false;
	}

	public static boolean isFreezing(Context context) {
		int accStatus = getAccStatus(context);
		if (ACC_FREEZING == accStatus) {
			return true;
		}
		return false;
	}

	public static boolean isIncomeForbidden(Context context) {
		int accStatus = getAccStatus(context);
		if (ACC_FORBIDDEN_BOTH == accStatus || ACC_FORBIDDEN_IN == accStatus
				|| ACC_FREEZING == accStatus) {
			return true;
		}
		return false;
	}

	public static boolean isOutlayForbidden(Context context) {
		int accStatus = getAccStatus(context);
		if (ACC_FORBIDDEN_BOTH == accStatus || ACC_FORBIDDEN_OUT == accStatus
				|| ACC_FREEZING == accStatus) {
			return true;
		}
		return false;
	}

	public static String getAccStatusName(Context context) {
		switch (getAccStatus(context)) {
		case ACC_OK:
			return "正常";
		case ACC_FORBIDDEN_IN:
			return "止入";
		case ACC_FORBIDDEN_OUT:
			return "止出";
		case ACC_FREEZING:
			return "冻结";
		case ACC_FORBIDDEN_BOTH:
			return "止入止出";
		default:
			return "未知";

		}

	}

	public static int getAccStatus(Context context) {
		if (isLogin(context)) {
			SharedPreferences share = context.getSharedPreferences(
					"MEMBER_USER", 0);
			String userInfoStr = share.getString("account_user_info", "");
			try {
				JSONObject data = new JSONObject(userInfoStr);
				if (data.has("accStatus")) {
					return data.getInt("accStatus");
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return -1;
	}

	public static JSONObject getUserInfo(Context context) {
		SharedPreferences share = context.getSharedPreferences("MEMBER_USER", 0);
		String userInfoStr = share.getString("account_user_info", "");
		if ("".equals(userInfoStr)) {
			return null;
		} else {
			try {
				JSONObject json = new JSONObject(userInfoStr);
				json.remove("result");
				return json;
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				return null;
			}
		}
	}
	
	public static String getUserCode(Context context)
    {
        try
        {
            JSONObject userJson = MemberService.getUserInfo(context);
            
            if(userJson != null)
            {
                return userJson.getString("accName");
            }
            
            return null;
        }
        catch (JSONException e)
        {
            e.printStackTrace();
            return null;
        }
    }
}
